package 力扣日常刷题.木22二月.第09天215;

/**
 * @author 帅小伙
 * @date 2022/2/15
 * @description 中   小  大
 */
public class Demo62二叉搜索树的先序遍历序列 {


    public static void main(String[] args) {
        System.out.println(verifyPostorder(new int[]{5, 2, 1, 3, 6}));
    }

    public static boolean verifyPostorder(int[] postorder) {
        return recur(postorder, 0, postorder.length - 1);
    }

    public static boolean recur(int[] postorder, int i, int j) {
        if (i >= j) return true;
        int p = j;
        while (postorder[p] > postorder[i]) p--;
        int m = p;
        while (postorder[p] < postorder[i]) p--;
        return p == i && recur(postorder, i + 1, m) && recur(postorder, m + 1, j);

    }


}
